<?php
class Form{

	public $controller;

	public function __construct($controller){
		$this->controller = $controller;
	}

	public function input($name,$label, $options = array()){
		$error = false;
		$classError = '';
		if(isset($this->errors[$name])){
			$error = $this->errors[$name];
			$classError = ' error';
		}

		if(!isset($this->controller->request->data->$name)){
			if((isset($options['type'])) &&  ($options['type'] == 'datetimepicker')){
				$value = date('m/d/Y 00:00');
			}else{
				$value = '';
			}		
		}
		else
		{
			$value = $this->controller->request->data->$name;
		}
		if($label == 'hidden'){
			return '<input type="hidden" name="'.$name.'" value="'.$value.'">';
		}
		$html = '<div class="clearfix">
					<label class="col-mg-4 control-label" for="input'.$name.'">'.$label.'</label>
					<div class="input">';
		$attr = ' ';
		//Si ce n'est pas le select des départements
		if (isset($options['type']) && ($options['type'] != 'select') && ($options['type'] != 'select_categ')){
			foreach($options as $k=>$v){ if ($k!='type'){
				$attr .= " $k=\"$v\"";
			}}
		}
		if($options['type'] == 'text'){

			$html .= '<input type="text" class="form-control" id="input'.$name.'" name="'.$name.'" value="'.$value.'"'.$attr.'>';
		}
		elseif($options['type'] == 'textarea'){
			$html .= '<textarea id="input'.$name.'" name="'.$name.'"'.$attr.'>'.$value.'</textarea>';
		}
		elseif($options['type'] == 'select'){

			$dpt = $options['departements'];

			$html .= '<select class="form-control" id="input'.$name.'" name="'.$name.'">';
				foreach ($dpt as $k => $v){
					if($v->numDepartement === $value){
						$html .= '<option selected="selected" value="'.$v->numDepartement.'">'.$v->numDepartement.' - '.$v->nom.'</option>';
					}else{
						$html .= '<option value="'.$v->numDepartement.'">'.$v->numDepartement.' - '.$v->nom.'</option>';
					}
				}
			$html .= '</select>';
		}
		elseif($options['type'] == 'select_categ'){

			$dpt = $options['categories'];

			$html .= '<select class="form-control" id="input'.$name.'" name="'.$name.'">';
				foreach ($dpt as $k => $v){
					if($v->id === $value){
						$html .= '<option selected="selected" value="'.$v->id.'">'.$v->id.' - '.$v->nom.'</option>';
					}else{
						$html .= '<option value="'.$v->id.'">'.$v->id.' - '.$v->nom.'</option>';
					}
				}
			$html .= '</select>';
		}
		elseif($options['type'] == 'checkbox'){
			$html .= '<input type="hidden" name="'.$name.'" value="0"><input type="checkbox" name="'.$name.'" value="1" '.(empty($value)?'':'checked').'>';
		}
		elseif($options['type'] == 'geolocalisation'){
			$html .= '<div class="input-group"><input type="text" class="form-control" id="input'.$name.'" name="'.$name.'" value="'.$value.'"'.$attr.'>';
			$html .= '<span class="input-group-btn"><button class="btn btn-default" type="button" id="getCoord">Lancer la géolocalisation</button></span></div>';
		}
		elseif($options['type'] == 'file'){
			$html .= '<input type="file" class="input-file" id="input'.$name.'" name="'.$name.'" value="'.$value.'"'.$attr.'>';
		}
		elseif($options['type'] == 'datetimepicker'){
			$html .= "<div class='input-group date' id=\"input".$name.'" name="'.$name.'">';
            $html .= "<input type='text' class=\"form-control\" id=\"input".$name.'" name="'.$name.'" value="'.date('d/m/Y H:i', strtotime($value)).'" />';
            $html .= "<span class=\"input-group-addon\"><span class=\"glyphicon glyphicon-calendar\"></span></span></div>";
		}
		elseif(!isset($options['password'])){
			$html .= '<input type="password" class="form-control" id="input'.$name.'" name="'.$name.'" value="'.$value.'"'.$attr.'>';
		}
		if($error){
			$html .= '<span class="help-block">'.$error.'</span';
		}
		$html .= '</div></div>';
		return $html;

	}
}